//
//  main.m
//  对象作为方法的参数连续传递
//
//  Created by 郭志远 on 2023/11/24.
//

#import <Foundation/Foundation.h>

#pragma mark - 弹夹
@interface Clip :NSObject
{
    @public
    int _bullet;
}
- (void)addBullet;
@end

@implementation Clip
- (void)addBullet
{
    _bullet = 10;
}
@end

#pragma mark - 枪
@interface Gun : NSObject
{
//    @public
//    int _bullet;
    Clip *clip;
}

-(void)shoot;
-(void)shoot:(Clip *)c;

@end

@implementation Gun
-(void)shoot
{
//    if (_bullet > 0) {
//        NSLog(@"打了一枪%i", _bullet);
//        _bullet--;
//    }else{
//        NSLog(@"没有子弹开不出来");
//    }

}

-(void)shoot:(Clip *)c
{
    if (c != nil && c->_bullet > 0) {
        c->_bullet -= 1;
        NSLog(@"打了一枪%i", c->_bullet);
    }else{
        NSLog(@"没有子弹开不出来");
    }
}

@end

#pragma mark - bingaa
@interface Soldier : NSObject
{
    @public
    NSString * _name;
    double _height;
    double _weight;
}
-(void)fire:(Gun *)gun;
-(void)fire:(Gun *)gun clip:(Clip *)clip;

@end

@implementation Soldier
-(void)fire:(Gun *)gun
{
    [gun shoot];
    
}

-(void)fire:(Gun *)gun clip:(Clip *)clip
{
    if (clip != nil && gun != nil) {
        [gun shoot:clip];
    }
}
@end

#pragma mark -
int main(int argc, const char * argv[]) {

    Soldier *sp = [Soldier new];
    sp->_name = @"asas";
    sp->_height = 12.2;
    sp->_weight = 21.2;
    
    Gun *gp = [Gun new];
    
    Clip *c = [Clip new];
    [c addBullet];
    
    [sp fire:gp clip:c];
    
    return 0;
}
